<?php
declare (strict_types = 1);

namespace app\command;

use app\model\zs\ZsMealDetailsStore;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

/**
 * 未完成，弃！
 */
class MealCalcTicketAmount extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('meal:calc_ticket_amount')
            ->setDescription('更新2021年4月30日之前的电子券金额（减去美食立减金额）');
    }

    protected function execute(Input $input, Output $output)
    {
        $num = 0;
        ZsMealDetailsStore::where('create_at', '<', '2021-05-01 00:00:00')
            ->with('businessPrize')
            ->chunk(1000, function ($list_detail) use (&$num) {
                foreach ($list_detail as $item_detail) {
                    $businessPrize = $item_detail->businessPrize;
                    $p_amount = $businessPrize->p_amount ?? 0;
                    $btype = $businessPrize->btype ?? '';
                    // 百货的跳过
                    if (!$p_amount || $btype == 1) {
                        continue;
                    }
                    // dump($item_detail->order_on);
                    // die;
                    if ($item_detail->ticket_amount >= $p_amount) {
                        $item_detail->ticket_amount_test = $item_detail->ticket_amount - $p_amount;
                        $item_detail->save();
                    } else {
                        $item_detail->ticket_amount_test = 0;
                        $item_detail->save();
                        $businessPrize->p_amount_test = $item_detail->amount;
                        $businessPrize->save();
                    }
                    ++$num;
                }
            }, 'id', 'desc');

        // 指令输出
        $output->writeln("更新 {$num} 条");
    }
}
